# coding:utf-8
import json, os

class HandleJson:

    def __init__(self, file_name='api_respond_info.json'):
        self.file_path = os.path.dirname(os.path.dirname(__file__)) + f'/configs/{file_name}'
        self.data = self.read_data()


    def read_data(self):
        '''
        读取json文件
        '''
        try:
            with open(self.file_path, encoding='utf-8') as fp:
                data = json.load(fp)
                return data
        except:
            return None

    def get_data(self, id):
        '''
        #根据关键字获取数据
        '''
        return self.data[id]

    def write_data(self, data):
        '''
        覆盖写入json文件
        '''
        with open(self.file_path, 'w+', encoding='utf-8') as fp:
            fp.write(json.dumps(data, ensure_ascii=False, indent=4, sort_keys=False))

    def clear_data(self):
        '''
        清空json文件数据
        '''
        with open(self.file_path, 'w+', encoding='utf-8') as fr:
            fr.truncate()




if __name__ == '__main__':
    hand_json = HandleJson()
    res = hand_json.clear_data()
    print(res)
